package com.acme.bankapp.domain.bank;

import java.io.Serializable;

/**
 * This class defines a client with one account.
 * Each client has name and gender
 * @author Artem Nikitin
 *
 */
public class Client implements Serializable{
	
	private static final long serialVersionUID = 1L;

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((account == null) ? 0 : account.hashCode());
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (gender != other.gender)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	private Account account;
	private String name;
	private Gender gender;
	
	/**
	 * Constructs a client with specified name and gender
	 * @param name - name
	 * @param gender - gender
	 * @throws IllegalArgumentException - If name is {@code null} or empty or gender is {@code null}
	 */
	public Client(String name, Gender gender) {
		if (name == null || name.isEmpty()) {
			throw new IllegalArgumentException("Name cannot be empty");
		}
		if (gender == null) {
			throw new IllegalArgumentException("gender must be not null");
		}
		this.name = name;
		this.gender = gender;
	}

	public Account getAccount() {
		return account;
	}
	
	public void setAccount(Account account) {
		this.account = account;
	}
	
	public String getClientSalutation() {
		return this.gender.honorific() + " " + this.name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		if (name == null || name.isEmpty()) {
			throw new IllegalArgumentException("Name cannot be empty");
		}
		this.name = name;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		if (gender == null) {
			throw new IllegalArgumentException("gender must be not null");
		}
		this.gender = gender;
	}
	
	@Override
	public String toString() {
		String balance = account != null ? Double.toString(account.getBalance()) : "N/A";
		return "Client: " + getClientSalutation() + ". Balance : " + balance;
	}
}
